package com.lpb.spring.test1;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.lpb.spring.lpbspring.context.AnnotationApplicationContext;
import com.lpb.spring.lpbspring.context.ApplicationContext;
import com.lpb.spring.lpbspring.getway.util.IdUtils;
import com.lpb.spring.test.TestCLa;
import com.lpb.spring.test.UserBizImpl;
import org.apache.commons.collections.CollectionUtils;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;

/**
 * @ClassName Test
 * @Description TODO
 * @date 2022/12/25 15:39
 * @Version 1.0
 * @authoe linpengbo
 */
public class Test {

    public static void main(String[] args) throws Exception {
        ApplicationContext context=new AnnotationApplicationContext("com.lpb.spring.test");
        UserBizImpl bean = context.getBean(UserBizImpl.class);
        String userName = bean.getUserName();
        System.out.println(userName);
       // setSeqAndCode("32","32");
        String s = nowDateTime();
        String currentDateLastDateTime = nowDate()+" 03:59:59";
        System.out.println(s );
        System.out.println(currentDateLastDateTime );
        Date d1 = parseDatetime(s, "yyyy-MM-dd HH:mm:ss");
        Date d2 = parseDatetime(currentDateLastDateTime, "yyyy-MM-dd HH:mm:ss");
        System.out.println((d2.getTime()-d1.getTime()));
        System.out.println((d2.getTime()-d1.getTime())/1000);
        String str="com.lpcc.open.inner.service.liepin.LiepinbdServicelookupIndustries";
        String substring = str.substring(str.lastIndexOf(".")+1);
        System.out.println(substring);
        Date yyyyMMddHHmmss = parseDatetime(getDate(-30) + "000000", "yyyyMMddHHmmss");
        String start = getDate(-30);
        System.out.println(start);
        String end= getDate(0);
        System.out.println(end);
        System.out.println(DateUtil.between(parse(start, "yyyyMMdd"),parse(end, "yyyyMMdd"),DateUnit.DAY));
        System.out.println(IdUtils.getId("c11b486c0b2419393f8a2df4717f4be0"));
        BigDecimal v1 = new BigDecimal(String.valueOf(0.003));
        BigDecimal v2 = new BigDecimal(String.valueOf(0.25));
        double avgValMoney= (0.003*0.25);  //v1.multiply(v2).setScale(4, BigDecimal.ROUND_DOWN).doubleValue();
        System.out.println(BigDecimal.valueOf(avgValMoney).setScale(4,4).toString());


        List<TestCLa> moduleVos = JSON.parseArray("[]", TestCLa.class);
        System.out.println(JSONUtil.toJsonStr(moduleVos));
        moduleVos.forEach(vo -> vo.setAge(1));
        moduleVos.sort((o1, o2) -> o2.getAge().compareTo(o1.getAge()));
        System.out.println(JSONUtil.toJsonStr(moduleVos));
        //System.out.println(IdUtils.getId("c11eaab98c221349817e0018ebf21c2e"));
        System.out.println(IdUtils.getId("c125ca7ale2385685745548f890dc77a"));
        System.out.println("c125ca7ale2385685745548f890dc77a".matches("\\d+"));

//        System.out.println(30f/100);
//        Map<Long, List<String>> twoTargetQuestionMap= Maps.newHashMap();
//        List<String> questionTitleList= Lists.newArrayList("就算时间再紧，我也会尽量把文档的格式调整规范。","我容易因为手头有太多事要处理而忘记别人托付的事。","手头的工作再多，我也会把每件事都做到最好。","我总能按时完成任务，从来不会逾期。","我会有选择性地只做好对我来说重要的工作。","我很少把事情拖到非做不可的时候才做。","即使通宵加班，我也会在规定时间内完成工作。","在工作中，只要大体方向不错就可以了。");
//        twoTargetQuestionMap.put(291L,questionTitleList);
//        List<String> sqlHead=Lists.newArrayList();
//        String zrdd = generalNsrlSql(27L, 291L, "zrdd", twoTargetQuestionMap, sqlHead);
//        String collect = sqlHead.stream().collect(Collectors.joining("\t"));
//        collect="--head:\""+collect+"\"";
//        System.out.println(collect);
//        System.out.println(zrdd);
    }

    public static double retail(double f, int decimalCount) {
        BigDecimal b = new BigDecimal(f);
        double f1 = b.setScale(decimalCount, 4).doubleValue();
        return f1;
    }

    private static String  generalNsrlSql(Long productId, Long twoTargetId, String alas, Map<Long, List<String>> twoTargetQuestionMap,List<String> sqlHead){
        List<String> questionTitleList = twoTargetQuestionMap.get(twoTargetId);
        if(CollectionUtils.isEmpty(questionTitleList)){

        }
        //责任担当原始分 责任担当9分制分值 责任担当百分制分值 责任担当题目数量  责任担当完成试题数  责任担当完成百分比
      String s=  org.apache.commons.lang3.StringUtils.join("max(case when product_id=",productId," and two_target_id=",twoTargetId,"  then origin_score else null end) as ",alas,"_score_origin,\n")+
        org.apache.commons.lang3.StringUtils.join("max(case when product_id=",productId," and two_target_id=",twoTargetId,"  then score_nine else null end) as ",alas,"_score_nine,\n")+
        org.apache.commons.lang3.StringUtils.join("max(case when product_id=",productId," and two_target_id=",twoTargetId,"  then score_percent else null end) as ",alas,"_score_percent,\n")+
        org.apache.commons.lang3.StringUtils.join("max(case when product_id=",productId," and two_target_id=",twoTargetId,"  then question_num else null end) as ",alas,"_question_num,\n")+
        org.apache.commons.lang3.StringUtils.join("max(case when product_id=",productId," and two_target_id=",twoTargetId,"  then finish_count else null end) as ",alas,"_finish_count,\n")+
        org.apache.commons.lang3.StringUtils.join("max(case when product_id=",productId," and two_target_id=",twoTargetId,"  then finish_percent else null end) as ",alas,"_finish_percent,\n");
        sqlHead.add(alas+"原始分");
        sqlHead.add(alas+"9分制分值");
        sqlHead.add(alas+"百分制分值");
        sqlHead.add(alas+"题目数量");
        sqlHead.add(alas+"完成试题数");
        sqlHead.add(alas+"完成百分比");
      StringBuilder sb=new StringBuilder();
            for (int i = 0; i < questionTitleList.size(); i++) {
                String questionTitle = questionTitleList.get(i);
                sb.append(org.apache.commons.lang3.StringUtils.join("max(case when product_id=",productId," and two_target_id=",twoTargetId," and question_title='",questionTitle,"'  then question_score else null end) as ",alas,"_question_score",i,",\n"));
                sqlHead.add("(得分)"+questionTitle);
                sb.append(org.apache.commons.lang3.StringUtils.join("max(case when product_id=",productId," and two_target_id=",twoTargetId," and question_title='",questionTitle,"'  then option_title else null end) as ",alas,"_option_title",i,",\n"));
                sqlHead.add("(选择选项)"+questionTitle);
            }

        return s+sb.toString();
    }

    public static String format(Date date, String format) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        LocalDateTime dateTime = LocalDateTime.of(
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH) + 1,
                calendar.get(Calendar.DAY_OF_MONTH),
                calendar.get(Calendar.HOUR_OF_DAY),
                calendar.get(Calendar.MINUTE),
                calendar.get(Calendar.SECOND),
                calendar.get(Calendar.MILLISECOND)
        );
        return dateTime.format(createCacheFormatter(format));
    }

    public static Date addHours(Date date, int amount) {
        return add(date, 11, amount);
    }

    private static Date add(Date date, int calendarField, int amount) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(calendarField, amount);
        return c.getTime();
    }

    public static String getDate(int offday) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, offday);

        String year = String.valueOf(cal.get(Calendar.YEAR));
        String month = String.valueOf(cal.get(Calendar.MONTH) + 1);
        if (month.length() == 1) {
            month = "0" + month;
        }
        String day = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
        if (day.length() == 1) {
            day = "0" + day;
        }

        return year + month + day;
    }

    public static String nowDate() {
        return LocalDate.now().toString();
    }
    public static String nowDateTime() {
        DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
        return LocalDateTime.now().format(createCacheFormatter("yyyy-MM-dd HH:mm:ss"));
    }
    private static DateTimeFormatter createCacheFormatter(String pattern){
        return DateTimeFormatter.ofPattern(pattern);
    }

    public static Date parseDatetime(String dateStr, String pattern) {
        return Date.from(LocalDateTime.parse(dateStr, createCacheFormatter(pattern)).atZone(ZoneId.systemDefault()).toInstant());
    }

    public static Date parse(String dateStr, String pattern) {
        return Date.from(LocalDate.parse(dateStr, createCacheFormatter(pattern)).atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
    }

    public static void setSeqAndCode(String pCode, String maxCode) {
        int seq = 1;
        String code;
        if (StringUtils.isNotBlank(maxCode)) {
            code = StringUtils.nextCode(maxCode);
            seq = Integer.valueOf(code.substring(code.length() - 2));
        }else {
            code = pCode + "01";
        }
        System.out.println(code);
        System.out.println(seq);
    }

}
